package stream_hub

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/tidwall/gjson"
	"io/ioutil"
	"net/http"
	"net/url"
	"time"
	"vrcm/src/vrcm/app/admin/models"
	dto "vrcm/src/vrcm/app/admin/service/dto/stream_hub"
	"vrcm/src/vrcm/app/admin/service/ngvsa"
	"vrcm/src/vrcm/common/service"
	ext "vrcm/src/vrcm/envconfig"
)

var client = http.Client{}

type StreamHubService struct {
	service.Service
}

// CreateStreamHub 创建协助转
func (e *StreamHubService) CreateStreamHub(d *dto.StreamHubReq) (*dto.StreamHubRes, error) {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return nil, err
	}
	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}

	res := &dto.StreamHubRes{}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/streamhub/stream"
	Url, err := url.Parse(urlString)
	if err != nil {
		return nil, err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	e.Log.Trace("GetStreamHubInfo:", urlPath)
	// 参数
	data := make(map[string]interface{})
	data["name"] = ""
	data["description"] = d.Description
	data["type"] = d.Type
	data["input"] = d.Input
	data["output"] = d.Output
	data["outputStreamName"] = d.OutputStreamName
	data["callbackAddress"] = d.CallbackAddress
	// Marshal
	bytesData, _ := json.Marshal(data)
	e.Log.Trace(string(bytesData))
	// 获取 request请求
	start := time.Now()
	request, err := http.NewRequest("POST", urlPath, bytes.NewReader(bytesData))
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil, err
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return nil, err
	}
	duration := time.Since(start)
	e.Log.Trace("调用协转任务耗时:", duration)
	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return res, err
	}
	e.Log.Trace("CreateStreamHub:", string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() == 30012 {
		return res, errors.New("自定义路径重复")
	}

	if result.Get("code").Int() != http.StatusOK {
		return res, errors.New(result.Get("message").String())
	}

	err = json.Unmarshal([]byte(result.Get("data").String()), res)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return res, err
	}
	return res, nil
}

// UpdateStreamHub 更新协转
func (e *StreamHubService) UpdateStreamHub(d *dto.StreamHubReq) (*dto.StreamHubRes, error) {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return nil, err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}

	res := &dto.StreamHubRes{}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/streamhub/stream/"
	urlString = urlString + d.Id
	Url, err := url.Parse(urlString)
	if err != nil {
		return nil, err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	// 参数
	data := make(map[string]interface{})
	data["id"] = d.Id
	data["name"] = d.Name
	data["description"] = d.Description
	data["type"] = d.Type
	data["input"] = d.Input
	data["output"] = d.Output
	data["callbackAddress"] = d.CallbackAddress
	// Marshal
	bytesData, _ := json.Marshal(data)
	e.Log.Trace("UpdateStreamHub:", string(bytesData))
	// 获取 request请求
	request, err := http.NewRequest("PUT", urlPath, bytes.NewReader(bytesData))
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil, nil
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return nil, err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return res, err
	}
	e.Log.Trace("UpdateStreamHub:", string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() == 30008 {
		return res, errors.New("正在回收资源，请 30 秒后重试")
	}
	if result.Get("code").Int() != http.StatusOK {
		return res, errors.New(result.Get("message").String())
	}

	err = json.Unmarshal([]byte(result.Get("data").String()), res)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return res, err
	}
	//res.LiveId = result.Get("data").Get("id").String()
	//res.TenantId = d.TenantId
	//res.ExpireTime = d.ExpireTime
	//res.FlvUrl = gjson.Get(result.Get("data").String(), `pull_address.#[format="flv"].url`).String()
	//res.HlsUrl = gjson.Get(result.Get("data").String(), `pull_address.#[format="hls"].url`).String()
	//res.WebrtcUrl = gjson.Get(result.Get("data").String(), `pull_address.#[format="webrtc"].url`).String()
	//res.RtmpUrl = gjson.Get(result.Get("data").String(), `pull_address.#[format="rtmp"].url`).String()

	return res, nil
}

// StartStreamHub 开启协转
func (e *StreamHubService) StartStreamHub(d *dto.StreamHubReq) error {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}

	params := url.Values{}
	urlString := streamHubUrl + "/v1/streamhub/stream/"
	urlString = urlString + d.Id + "/start"
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	e.Log.Trace("StartStreamHub:", urlPath)
	// 获取 request请求
	request, err := http.NewRequest("PUT", urlPath, nil)
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	e.Log.Trace("StartStreamHub:", string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK && result.Get("code").Int() != 10000016 {
		return errors.New(result.Get("message").String())
	}

	if result.Get("code").Int() == 30008 {
		return errors.New("正在回收资源，请 30 秒后重试")
	}
	return nil
}

// StopStreamHub 停止协转
func (e *StreamHubService) StopStreamHub(d *dto.StreamHubReq) error {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/streamhub/stream/"
	urlString = urlString + d.Id + "/stop"
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	e.Log.Trace("StopStreamHub:", urlPath)
	// 获取 request请求
	request, err := http.NewRequest("PUT", urlPath, nil)
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	e.Log.Trace("StopStreamHub:", string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK && result.Get("code").Int() != 10000016 && result.Get("code").Int() != 404 {
		return errors.New(result.Get("message").String())
	}
	return nil
}

// DeleteStreamHub 删除协转
func (e *StreamHubService) DeleteStreamHub(d *dto.StreamHubReq) error {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/streamhub/stream/"
	urlString = urlString + d.Id
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	fmt.Println(urlPath)
	// 获取 request请求
	request, err := http.NewRequest("DELETE", urlPath, nil)
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	e.Log.Trace("DeleteStreamHub:", string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK && result.Get("code").Int() != 10000016 && result.Get("code").Int() != 404 {
		return errors.New(result.Get("message").String())
	}

	return nil
}

func (e *StreamHubService) RemoveStreamHub(d *dto.StreamHubReq) error {

	err := e.StopStreamHub(d)
	if err != nil {
		e.Log.Errorf("停止streamHub失败 error:%s", err)
		return err
	}
	err = e.DeleteStreamHub(d)
	if err != nil {
		e.Log.Errorf("删除streamHub失败 error:%s", err)
		return err
	}
	return nil
}

// CreateRTCLive 创建RTC直播
func (e *StreamHubService) CreateRTCLive(d *dto.StreamHubReq) error {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/streamhub/stream/"
	urlString = urlString + d.Id
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	fmt.Println(urlPath)
	// 获取 request请求
	request, err := http.NewRequest("DELETE", urlPath, nil)
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	fmt.Println(string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK && result.Get("code").Int() != 10000016 {
		return errors.New(result.Get("message").String())
	}

	return nil
}

// CreateTransCode 创建转码任务
func (e *StreamHubService) CreateTransCode(d *dto.TransCodeReq) (*dto.TransCodeRes, error) {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return nil, err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/mediatranscoder/stream"
	Url, err := url.Parse(urlString)
	if err != nil {
		return nil, err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	e.Log.Trace("CreateTransCode:", urlPath)

	// 参数
	data := make(map[string]interface{})
	data["name"] = ""
	data["type"] = d.Type
	data["input"] = d.Input
	data["output"] = d.Output
	data["callbackAddress"] = d.CallbackAddress
	data["ngvsaServiceUrl"] = d.NgvsaServiceUrl
	data["vrcServiceUrl"] = d.VrcServiceUrl
	// Marshal
	bytesData, _ := json.Marshal(data)
	// 获取 request请求
	request, err := http.NewRequest("POST", urlPath, bytes.NewReader(bytesData))
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil, err
	}
	e.Log.Trace(string(bytesData))
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return nil, err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return nil, err
	}
	e.Log.Trace("CreateTransCode :", string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK {
		return nil, errors.New(result.Get("message").String())
	}
	res := &dto.TransCodeRes{}
	err = json.Unmarshal([]byte(result.Get("data").String()), res)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return res, err
	}
	fmt.Println("res", res)
	return res, err
}

// StartTransCode 上线转码任务
func (e *StreamHubService) StartTransCode(d *dto.TransCodeReq) error {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/mediatranscoder/stream/"
	urlString = urlString + d.Id + "/start"
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	e.Log.Trace("StartTransCode转码：", urlPath)
	// 获取 request请求
	request, err := http.NewRequest("PUT", urlPath, nil)
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	e.Log.Trace(string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() == 30008 {
		return errors.New("正在回收资源，请 30 秒后重试")
	}
	if result.Get("code").Int() != http.StatusOK && result.Get("code").Int() != 10000016 {
		return errors.New(result.Get("message").String())
	}

	return nil
}

// StopTransCode 下线转码任务
func (e *StreamHubService) StopTransCode(d *dto.TransCodeReq) error {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/mediatranscoder/stream/"
	urlString = urlString + d.Id + "/stop"
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	fmt.Println(urlPath)

	// 获取 request请求
	request, err := http.NewRequest("PUT", urlPath, nil)
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	fmt.Println(string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK && result.Get("code").Int() != 10000016 && result.Get("code").Int() != 404 {
		return errors.New(result.Get("message").String())
	}

	return nil
}

// DeleteTransCode 删除转码任务
func (e *StreamHubService) DeleteTransCode(d *dto.TransCodeReq) error {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/mediatranscoder/stream/"
	urlString = urlString + d.Id
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	fmt.Println(urlPath)

	// 获取 request请求
	request, err := http.NewRequest("DELETE", urlPath, nil)
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	fmt.Println(string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK && result.Get("code").Int() != 10000016 && result.Get("code").Int() != 404 {
		return errors.New(result.Get("message").String())
	}

	return nil
}

// RemoveTransCodeTask 删除转码任务
func (e *StreamHubService) RemoveTransCodeTask(d *dto.TransCodeReq) error {

	err := e.StopTransCode(d)
	if err != nil {
		e.Log.Errorf("停止streamHub失败 error:%s", err)
		return err
	}
	err = e.DeleteTransCode(d)
	if err != nil {
		e.Log.Errorf("删除streamHub失败 error:%s", err)
		return err
	}
	return nil
}

// CreateSwitchSourceTask 创建切换信源任务
func (e *StreamHubService) CreateSwitchSourceTask(d *dto.SwitchSourceReq) (*dto.SwitchSourceRes, error) {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return nil, err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/mediaswitch/stream"
	Url, err := url.Parse(urlString)
	if err != nil {
		return nil, err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	e.Log.Trace("CreateSwitchSourceTask:", urlPath)

	// 参数
	data := make(map[string]interface{})
	data["name"] = ""
	data["type"] = d.Type
	data["input"] = d.SwitchInput
	data["serviceOutputAddress"] = d.OutputAddress
	data["callbackAddress"] = ext.ExtConfig.ServiceConfig.VrcmServiceUrl + "/api/v1/source-basic/auto-switch-callback"
	data["autoSwitch"] = d.AutoSwitch
	// Marshal
	bytesData, _ := json.Marshal(data)
	e.Log.Trace("CreateSwitchSourceTask:", string(bytesData))
	// 获取 request请求
	request, err := http.NewRequest("POST", urlPath, bytes.NewReader(bytesData))
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil, err
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return nil, err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return nil, err
	}
	e.Log.Trace("CreateSwitchSourceTask:", string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK {
		return nil, errors.New(result.Get("message").String())
	}
	res := &dto.SwitchSourceRes{}
	err = json.Unmarshal([]byte(result.Get("data").String()), res)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return res, err
	}
	return res, err
}

// UpdateSwitchSourceTask 编辑切换信源
func (e *StreamHubService) UpdateSwitchSourceTask(d *dto.SwitchSourceReq) (*dto.SwitchSourceRes, error) {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return nil, err
	}
	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}

	res := &dto.SwitchSourceRes{}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/mediaswitch/stream/"
	urlString = urlString + d.Id
	Url, err := url.Parse(urlString)
	if err != nil {
		return nil, err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()

	// 参数
	data := make(map[string]interface{})
	data["name"] = d.Name
	data["type"] = d.Type
	data["input"] = d.SwitchInput
	data["callbackAddress"] = ext.ExtConfig.ServiceConfig.VrcmServiceUrl + "/api/v1/source-basic/auto-switch-callback"
	data["autoSwitch"] = d.AutoSwitch
	// Marshal
	bytesData, _ := json.Marshal(data)
	fmt.Println(string(bytesData))
	// 获取 request请求
	request, err := http.NewRequest("PUT", urlPath, bytes.NewReader(bytesData))
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil, nil
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return nil, err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return res, err
	}
	e.Log.Trace("UpdateSwitchSourceTask", string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK {
		return res, errors.New(result.Get("message").String())
	}

	err = json.Unmarshal([]byte(result.Get("data").String()), res)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return res, err
	}

	return res, nil
}

// DeleteSwitchSourceTask 删除切换信源
func (e *StreamHubService) DeleteSwitchSourceTask(d *dto.SwitchSourceReq) error {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/mediaswitch/stream/"
	urlString = urlString + d.Id
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	fmt.Println(urlPath)
	// 获取 request请求
	request, err := http.NewRequest("DELETE", urlPath, nil)
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	e.Log.Trace(string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK && result.Get("code").Int() != 10000016 && result.Get("code").Int() != 404 {
		return errors.New(result.Get("message").String())
	}

	return nil
}

// SwitchSource 信源切换
func (e *StreamHubService) SwitchSource(d *dto.ChangeSourceReq) error {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/mediaswitch/stream/"
	urlString = urlString + d.Id + "/switch"
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	fmt.Println(urlPath)

	// 参数
	data := make(map[string]interface{})
	data["active"] = d.Active
	// Marshal
	bytesData, _ := json.Marshal(data)
	// 获取 request请求
	request, err := http.NewRequest("PUT", urlPath, bytes.NewReader(bytesData))
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	e.Log.Trace(string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK {
		return errors.New(result.Get("message").String())
	}

	return nil
}

// StartSwitchSource 上线信源切换
func (e *StreamHubService) StartSwitchSource(d *dto.StartOrStopSwitchSourceReq) error {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/mediaswitch/stream/"
	urlString = urlString + d.Id + "/start"
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	e.Log.Trace("启用切换任务地址：", urlPath)
	// 获取 request请求
	request, err := http.NewRequest("PUT", urlPath, nil)
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	e.Log.Trace(string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK {
		return errors.New(result.Get("message").String())
	}

	return nil
}

// StopSwitchSource 下线信源切换
func (e *StreamHubService) StopSwitchSource(d *dto.StartOrStopSwitchSourceReq) error {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/mediaswitch/stream/"
	urlString = urlString + d.Id + "/stop"
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	e.Log.Trace("停用切换任务地址：", urlPath)
	// 获取 request请求
	request, err := http.NewRequest("PUT", urlPath, nil)
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	e.Log.Trace(string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK && result.Get("code").Int() != 10000016 && result.Get("code").Int() != 404 {
		return errors.New(result.Get("message").String())
	}

	return nil
}

// EditRefinePlayUrl 更新自定义参数
func (e *StreamHubService) EditRefinePlayUrl(d *dto.EditRefinePlayUrlReq) error {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return err
	}

	//streamHubUrl := ""
	//if ext.ExtConfig.ServiceConfig.Mode == models.DevelopmentMode {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.DevNgvsaStationUrl
	//} else {
	//	streamHubUrl = ext.ExtConfig.ServiceConfig.ProNgvsaStationUrl
	//}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/streamhub/stream/output/"
	urlString = urlString + d.Id + "/editRefinePlayUrl"
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	e.Log.Trace("更新自定义参数地址：", urlPath)

	// 参数
	data := make(map[string]interface{})
	data["redefinePlayUrlEnable"] = d.RedefinePlayUrlEnable
	data["redefinePlayUrlParams"] = d.RedefinePlayUrlParams
	// Marshal
	bytesData, _ := json.Marshal(data)
	// 获取 request请求
	request, err := http.NewRequest("PUT", urlPath, bytes.NewReader(bytesData))
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil
	}
	e.Log.Trace(string(bytesData))
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	e.Log.Trace(string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK {
		return errors.New(result.Get("message").String())
	}

	return nil
}

func (e *StreamHubService) GetTransCodeTemplate() (*[]dto.TemplateInfo, error) {
	res := make([]dto.TemplateInfo, 0, 5)
	urlPath := ext.ExtConfig.ServiceConfig.NgvsaServiceUrl + "/v1/template?type=transcoder"
	e.Log.Trace("GetTransCodeTemplate：", urlPath)
	// 获取 request请求
	request, err := http.NewRequest("GET", urlPath, nil)
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return &res, nil
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return &res, err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return &res, err
	}
	e.Log.Trace(string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK {
		return &res, errors.New(result.Get("message").String())
	}

	err = json.Unmarshal([]byte(result.Get("data").Get("rows").String()), &res)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
	}

	return &res, nil
}

func (e *StreamHubService) CreateConvertTask(d *dto.ConvertParamReq) (*dto.StreamHubRes, error) {
	target := dto.Target{}
	target.Format = d.OutputProtocol
	//输入参数

	sourceParams := make([]dto.Source, 0, 3)
	for _, v := range d.InputUrls {
		v := v
		sourceParam := dto.Source{}
		sourceParam.Url = v
		sourceParams = append(sourceParams, sourceParam)
	}

	req := dto.StreamHubReq{}
	req.Name = ""
	req.Type = "live"
	req.TenantId = d.TenantId
	req.Description = ""
	req.Input = dto.Input{
		Format: d.InputProtocol,
		Active: 0,
		Policy: dto.Policy{
			Mode: "manual",
		},
		Type:    d.InputType,
		Sources: sourceParams,
	}

	//第三方拉流+内网 输出类型为pull 其他情况为push

	if d.StreamType == 2 && d.Area == 2 {
		target.Type = "pull"
	} else {
		target.Type = "push"
	}
	delay := dto.Delay{
		Enable:   "no",
		Duration: 10,
	}

	//req.CallbackAddress = ext.ExtConfig.ServiceConfig.VrcmServiceUrl + "/api/v1/channel-task/convert-task-callback"

	if len(d.OutputUrls) > 0 {
		target.Url = d.OutputUrls[0]
	}
	target.RedefinePlayUrlEnable = "2"
	params := dto.RedefinePlayUrlParams{}
	target.RedefinePlayUrlParams = params

	//创建协助任务
	req.Output = dto.Output{
		Targets: []dto.Target{target},
		Delay:   delay,
		Acl:     dto.Acl{},
	}

	res, err := e.CreateStreamHub(&req)
	if err != nil {
		e.Log.Errorf("CreateStreamHub error:", err)
		return res, err
	}

	return res, err
}

func (e *StreamHubService) SetHlsWhiteBlack(d *dto.SetBlackWhiteParam) error {
	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return err
	}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/streamhub/stream/output/" + d.TaskId + "/editACL"
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	e.Log.Trace("SetHlsWhiteBlack:", urlPath)
	// 参数
	data := make(map[string]interface{})
	data["acl"] = d.Acl
	// Marshal
	bytesData, _ := json.Marshal(data)
	// 获取 request请求
	request, err := http.NewRequest("PUT", urlPath, bytes.NewReader(bytesData))
	request.Header.Set("Authorization", "Bearer "+models.SuperToken)
	request.Header.Set("X-PS-Flag", "")
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return err
	}
	e.Log.Trace(string(bytesData))
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	e.Log.Trace("SetHlsWhiteBlack :", string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK {
		return errors.New(result.Get("message").String())
	}
	return err
}

// DispatchLimit 分发限额
func (e *StreamHubService) DispatchLimit(d *dto.StreamHubReq) (*dto.DisPatchLimitRes, error) {

	ngvsaService := ngvsa.NgvsaService{}
	ngvsaService.Log = e.Log
	streamHubUrl, err := ngvsaService.GetStreamHubInfo(d.TenantId, "tenant", "station")
	if err != nil {
		e.Log.Errorf("获取streamHubUrl失败 error:%s", err)
		return nil, err
	}
	res := &dto.DisPatchLimitRes{}
	params := url.Values{}
	urlString := streamHubUrl + "/v1/request_scheduler"
	Url, err := url.Parse(urlString)
	if err != nil {
		return nil, err
	}
	params.Set("tenantId", d.TenantId)
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	e.Log.Trace("DispatchLimit:", urlPath)
	// 获取 request请求
	request, err := http.NewRequest("GET", urlPath, nil)
	request.Header.Set("Authorization", "Bearer "+models.SuperToken)
	request.Header.Set("X-PS-Flag", "")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return nil, err
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return nil, err
	}
	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return res, err
	}
	e.Log.Trace("DispatchLimit:", string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK {
		return res, errors.New(result.Get("message").String())
	}
	err = json.Unmarshal([]byte(result.Get("data").String()), res)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return res, err
	}
	return res, nil
}
